/*
 *  Query.java        22-ott-2009
 *   
 * Copyright (c) 2009 mentalsmash.org
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 *  The use of the Apache License does not indicate that this project is
 *  affiliated with the Apache Software Foundation.
 */

package org.mentalsmash.tazio.transactions.management;

import java.io.Serializable;

import org.mentalsmash.documentation.Concurrency;
import org.mentalsmash.tazio.commons.identifiers.OID;
import org.mentalsmash.tazio.transactions.objects.RootSet;

/**
 * Represents a read operation on a <tt>TransactionalObject</tt>
 * 
 * @author Andrea Reale
 * @version 0.0.1
 */

@Concurrency(concurrentAccess = false, specification = "Query is not synchronized. User of this container class must"
	+ " provide synchronization on access to those instances."

)
public class Query extends TransactionalOperation {

    private static final long serialVersionUID = -4929102372635935957L;
    // I don't need to transmit what was the value that was written
    private transient Serializable readValue = null;

    /**
     * @param targetObject
     * @param originalVersion
     */
    public Query(final OID targetObject, final long originalVersion) {
	super(targetObject, originalVersion,false);
    }

    /**
     * Set the value that was read by this query
     * 
     * @param readValue
     *            the readValue to set
     */
    public void setReadValue(final Serializable readValue) {
	this.readValue = readValue;
    }

    /**
     * Returns the value that was read by this query
     * 
     * @return the readValue
     */
    public Serializable getReadValue() {
	return this.readValue;
    }

    @Override
    public int hashCode() {
	final int prime = 31;
	int result = super.hashCode();
	result = prime * result
		+ ((this.readValue == null) ? 0 : this.readValue.hashCode());
	return result;
    }

    @Override
    public boolean equals(final Object obj) {
	if (this == obj) {
	    return true;
	}
	if (!super.equals(obj)) {
	    return false;
	}
	if (getClass() != obj.getClass()) {
	    return false;
	}
	final Query other = (Query) obj;
	if (this.readValue == null) {
	    if (other.readValue != null) {
		return false;
	    }
	} else if (!this.readValue.equals(other.readValue)) {
	    return false;
	}
	return true;
    }

    @Override
    public String toString() {
	final StringBuilder builder = new StringBuilder();
	builder.append("Query [readValue=");
	builder.append(this.readValue);
	builder.append(", propertyName()=");
	builder.append(getPropertyName());
	builder.append(", targetObject()=");
	builder.append(getTargetObject());
	builder.append("]");
	return builder.toString();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mentalsmash.tazio.transactions.TransactionalOperation#commitOperation
     * (org.mentalsmash.tazio.transactions.RootSet)
     */
    @Override
    public void commitOperation(final RootSet rs) {
	// nothing to do
	log.trace("Nothing to do to commit a query");
    }

}
